package com.kaishengit.action;

import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.kaishengit.pojo.User;
import com.kaishengit.service.UserService;

@Results({
	@Result(name="usersuccess",type="redirectAction",location="project"),
	@Result(name="usererror",type="redirectAction",location="index?state=10000"),
	@Result(name="relogin",type="redirectAction",location="index?state=10001"),
	@Result(name="add",type="redirectAction",location="adduser?state=20000")
})
public class IndexAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private UserService userService;
	private User user;
	
	public String list() throws Exception {
		return "success";
	}
	
	@Action("login")
	public String login(){
		user = userService.findByNameAndPwd(user);
		if(user != null){
			putInSession("user", user);
			return "usersuccess";
		}else{
			return "usererror";
		}
	}
	@Action("adduser")
	public String add(){
		return "success";
	}
	@Action("saveuser")
	public String save(){
		userService.save(user);
		return "add";
	}
	
	@Action("edituser")
	public String edit(){
		user = (User) getFromSession("user");
		return "success";
	}
	
	@Action("updateuser")
	public String update(){
		userService.update(user);
		return "relogin";
	}
	
	
	public UserService getUserService() {
		return userService;
	}
	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}

	public void setApplication(Map<String, Object> arg0) {
	}
}
